<?php


namespace App\Dao;

use App\Model\NoticeCooperative;

class NoticeCooperativeDao extends AbstractDao
{
    /**
     * @param array $where
     * @return \Hyperf\Database\Model\Builder
     */
    public function where(array $where): \Hyperf\Database\Model\Builder
    {
        $query = NoticeCooperative::query();
        foreach ($where as $field => $value) {
            switch ($field) {
                case 'cooperative_user_ids':
                    if ($value) {
                        $query->whereIn('cooperative_user_id', $value);
                    }
                    break;
                case 'created_at_gt':
                    $query->where('created_at', '>', $value);
                    break;
                default:
                    if (is_array($value)) {
                        $query->where($field, $value[0], $value[1]);
                    } else {
                        $query->where($field, $value);
                    }
            }
        }
        return $query;
    }

    /**
     * 获取未读列表
     * @param $param
     * @return mixed
     */
    public function UnreadList($param)
    {
       $result = $this->where([
                'cooperative_user_id' => $param['user_id'],
                'status' => 0
           ])
           ->orderBy('created_at','DESC')
           ->paginate($param['per_page']??30,['*'],'page',$param['page']??1)
           ->toArray();
       return $result;
    }

    /**
     * 更新数据
     * @param $where
     * @param $data
     * @return int
     */
    public function UpdateNotice($where,$data)
    {
        return NoticeCooperative::query()->whereIn('id',$where)->update($data);
    }

}